<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: Ason
 * Date: 15-5-18
 * Time: 上午11:28
 * 后台管理控制器基类
 */

class WL_controller extends CI_Controller{

	public $data;
	
	function __construct(){
		parent::__construct();
		
		$this->load->model( 'site_model' );
		//网站基本信息
		$this->data[ 'site' ] = $this->site_model->get_site_info();
		$setting = $this->config->item( 'exam' );

		//后台风格目录
		$this->data[ 'tpl_dir' ] = base_url(). $setting['tpl_dir'] ;
		$this->data[ 'setting' ] = $setting;

		$this->load->model( 'sidebar_model', 'menu' ) ;

		
	}
	
	/*
	 * 用户是否已登录
	 * @return bool
	 * */
	protected function is_logged(){

		return sess( 'id' ) ? true : false;
	}

	/**
	 * 如果用户没登录，跳转到登录页
	 * */
	public function logged_check(){
		if( !$this->is_logged() )
			redirect( '/login' );
	}

}

class Admin_controller extends WL_controller {

	function __construct(){
		parent::__construct();
		//后台管理导航
		$this->get_menu();
		
		//验证控制器访问权限
		$this->check_access();
		//$this->data[ 'current_pos' ] = $this->current_pos()
		
	}

	/**
	 * 加载后台导航菜单
	 * */
	protected function get_menu(){
		if( sess( 'id' ) == 1 ){
			$this->data['menu'] = $this->menu->get_all_menu();
		}else{
			$this->data['menu'] = $this->menu->get_role_menu();
		}
		
	}
	
	/**
	 * 渲染视图
	 * @param $page string 视图文件名
	 * @param $data array 视图数据
	 * */
	protected function view( $page ){
		
		$this->data[ 'page' ] = $page;
		$this->load->view( 'frame', $this->data );
	}

	/**
	 * 验证用户是否具有控制器的访问权限
	 * @return bool
	 * */
	private function check_access(){
			
		$this->data[ 'router' ] = strtolower( $this->router->fetch_class() .'/'. $this->router->fetch_method() );
		if( sess( 'id' ) == 1 ){return TRUE;}
		
		//当前控制器在menu表中 && 在permit中，有权访问
		$menu = $this->menu->get_field( 'menu', array('id', 'url'), 'parent_id' )->result();
		foreach( $menu as $v ){
			$menu_url[ $v[ 'id' ] ] = $v[ 'url' ];
		}
		$menu_id = array_search( $this->data[ 'router' ], $menu_url );
		if( $menu_id ){
			$this->load->model( 'user_model' );
			$user = $this->user_model->get_user_by_id( sess( 'id' ) );
			$group = $this->user_model->get_group_by_id( $user->group );
			$permit = explode( ',', $group['permit'] );
			if( ! in_array( $menu_id, $permit ) )
				redirect( '/login' );
		}else{
			return TRUE;
		}
	}
	/**
	 * 中转页面
	 * @param $title 页面标题
	 * @param $content 内容
	 * @param $url 自动跳转url
	 * */
	public function redirect( $title = '', $content = '', $url = '' ){
		
		$this->data[ 'error' ][ 'title' ] = $title;
		$this->data[ 'error' ][ 'content' ] = $content;
		$this->data[ 'error' ][ 'url' ] = site_url( $url );
		$this->view( 'redirect' );
	}
	/**
	 * 拼接当前位置HTML
	 * @param $id int 菜单id
	 * @param $link bool 是否返回带连接的HTML
	 * */
	public function current_pos( $id = 0, $link = FALSE ){
		
		$id = (int)$id;
		$str = '';
		$arr = [];
		if( !$menu = $this->menu->get_menu_by_id( $id ) ){
			return FALSE;
		}
		if( $link ){
			$arr[] = '<li><a href=" ' . $menu[$id]['url'] .'" >' . $menu[$id]['name'] . '</a></li>';
		}else{
			$arr[] = '<li>' . $menu[$id]['name'] . '</li>';
		}

		if( $menu[ 'parent_id' ] ){
			array_unshift( $arr, $this->current_pos( $menu[ 'parent_id' ], TRUE ) );
		}
		return implode( $arr );
	}
}